package com.shanghaionstar.discover.charge.presenter;

import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.framework.mvp.base.IMVPScheduler;
import com.onstar.cn.framework.mvp.base.IMVPView;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.shanghaionstar.discover.charge.iview.IChargeModeNotificationView;
import com.shanghaionstar.discover.charge.service.ChargeNotificationService;

import rx.functions.Action1;

/**
 * Created by 7828wc on 16/7/21.
 */
public class ChargeModeNotificationPresenter extends BasePresenter<Class<?>> {
    private ChargeNotificationService service;
    private IChargeModeNotificationView view;

    public ChargeModeNotificationPresenter(IMVPScheduler mvpScheduler) {
        super(mvpScheduler);
    }

    @Override
    public void setView(IMVPView<Class<?>> view) {
        super.setView(view);
        this.view = (IChargeModeNotificationView) view;
        service = new ChargeNotificationService(view.onActivity());
    }

    public void putChargeMode(final boolean isClose) {
        view.setLoading(true);
        subscription = service.putChargeState(isClose).subscribeOn(mvpScheduler.backgroundThread())
                .observeOn(mvpScheduler.mainThread()).subscribe(new Action1<String>() {
                    @Override
                    public void call(String s) {
                        view.setLoading(false);
                        view.onPutMode(s,isClose);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        view.setLoading(false);
                        view.onPutFailed(isClose);
                    }
                });
    }

    public void loadChargeMode() {
        view.setLoading(true);
        subscription = service.loadChargeState().subscribeOn(mvpScheduler.backgroundThread())
                .observeOn(mvpScheduler.mainThread()).subscribe(new Action1<String>() {
                    @Override
                    public void call(String s) {
                        view.setLoading(false);
                        view.onLoadMode(s);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        view.setLoading(false);
                        view.onResponseError((OnStarException) throwable);
                    }
                });
    }

}
